<?php
  require_once('./db.php');
  require_once('./header.php');

  // searching Start
  if(isset($_GET['search']) && !empty($_GET['search'])) {
      $search = $_GET['search'];
      $where = "WHERE `uname` LIKE '%$search%'";
  } else {
    $search = '';
    $where = '';
  }
  // search end



  $usersQuery = "SELECT * FROM `users` {$where}";
  $usersResult = mysql_query($usersQuery) or die(mysql_error());

  $fileName = date('Ymdhis').'.csv';
  $fp = fopen("./tmp/$fileName",'w+');
  $colNamesArr = array(
                    'ID',
                    'USER NAME',
                    'EMAIL',
                    'PASSWORD',
                    'ADDED DATE',
                    'MODIFIED DATE',
                    'STATUS',
                    'AVATAR',
                  );
  fputcsv($fp, $colNamesArr);

  while($user = mysql_fetch_assoc($usersResult)) {
    fputcsv($fp, $user);
  }
  fclose($fp);
?>
<h4>Please <a href="tmp/<?=$fileName?>">click here</a> to download users.</h4>
<?php
  require_once('./footer.php');
